package com.aracky.system.service.impl;

import com.aracky.common.util.ComUtil;
import com.aracky.system.model.entity.SysPost;
import com.aracky.system.model.entity.SysUserPost;
import com.aracky.system.mapper.SysUserPostMapper;
import com.aracky.system.service.SysUserPostService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户与岗位关联表 服务实现类
 * </p>
 *
 * @author zzp
 * @since 2022-07-22 03:31:01
 */
@Service
public class SysUserPostServiceImpl extends ServiceImpl<SysUserPostMapper, SysUserPost> implements SysUserPostService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveWxUserPost(List<SysUserPost> sysUserPostList) {
        if(ComUtil.isNullList(sysUserPostList)){
            return;
        }
        //先删除用户与岗位关系的数据
        List<Long> userIdList = sysUserPostList.stream().map(SysUserPost::getUserId).collect(Collectors.toList());

        LambdaQueryWrapper<SysUserPost> postLambdaQueryWrapper =
                Wrappers.<SysUserPost>lambdaQuery().in(SysUserPost::getUserId, userIdList);
        this.remove(postLambdaQueryWrapper);

        //新增关系数据
        this.saveBatch(sysUserPostList);

    }
}
